//
//  MeSettingController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/14.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "MeSettingController.h"
#import "MeSettingCell.h"
#import "EditorPersonalInformation.h"
#import "AccountManagementController.h"
#import "TheBlacklistController.h"
#import "AboutUsController.h"

@interface MeSettingController ()<UITableViewDelegate,UITableViewDataSource>
/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataSource;

@end

@implementation MeSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的设置";
    [self.view addSubview:self.listView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndNotClearColor];
}

/**
 退出登录
 */
-(void)tapLogout{
    [UserModelManager loginout];
}
#pragma mark --UITableViewDelegate,UITableViewDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return ((NSArray *)self.dataSource[section]).count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MeSettingCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MeSettingCell class])];
    cell.indexPath = indexPath;
    cell.nameStr = self.dataSource[indexPath.section][indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        [self.navigationController pushViewController:[EditorPersonalInformation  new] animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        [self.navigationController pushViewController:[AccountManagementController  new] animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 2){
        [self.navigationController pushViewController:[TheBlacklistController  new] animated:YES];
    }else if (indexPath.section == 1 && indexPath.row == 0){
        [self clearCache];
    }else if (indexPath.section == 1 && indexPath.row == 1){
        [self setFont];
    }
    
    if (indexPath.section == 2) {
        [self.navigationController pushViewController:[AboutUsController  new] animated:YES];
    }
}
/**
 清除缓存
 */
-(void)clearCache{
    FJAlertController * alerCon = [FJAlertController alertControllerWithTitle:@"" message:@"确定删除所有缓存？问答草稿·离线内容及图" preferredStyle:UIAlertControllerStyleActionSheet];
    FJAlertAction * action = [FJAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    FJAlertAction * action1 = [FJAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    action.textColor = UIColorFromRGB(0x009966);
    action1.textColor = UIColorFromRGB(0x009966);

    
    [alerCon addAction:action];
    [alerCon addAction:action1];

    [self presentViewController:alerCon animated:YES completion:nil];
}
-(void)setFont{
    FJAlertController * alerCon = [FJAlertController alertControllerWithTitle:@"" message:@"设置字体大小" preferredStyle:UIAlertControllerStyleActionSheet];
    FJAlertAction * action = [FJAlertAction actionWithTitle:@"小" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    FJAlertAction * action1 = [FJAlertAction actionWithTitle:@"中" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    FJAlertAction * action2 = [FJAlertAction actionWithTitle:@"大" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    FJAlertAction * action3 = [FJAlertAction actionWithTitle:@"特大" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];

    FJAlertAction * cancel = [FJAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alerCon addAction:action];
    [alerCon addAction:action1];
    [alerCon addAction:action2];
    [alerCon addAction:action3];

    [alerCon addAction:cancel];
    
    action.textColor = UIColorFromRGB(0x009966);
    action1.textColor = UIColorFromRGB(0x009966);
    action2.textColor = UIColorFromRGB(0x009966);
    action3.textColor = UIColorFromRGB(0x009966);
    cancel.textColor = UIColorFromRGB(0x009966);

    
    [self presentViewController:alerCon animated:YES completion:nil];
}
#pragma mark ---懒加载
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _listView.dataSource = self;
        _listView.delegate =self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView registerClass:[MeSettingCell class] forCellReuseIdentifier:NSStringFromClass([MeSettingCell class])];
        _listView.tableFooterView = [self createFootView];
        [_listView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        _listView.separatorColor = SeparatorColor;
        _listView.backgroundColor = UIColorFromRGB(0xF5F7F5);
        
    }
    return _listView;
}
-(UIView *)createFootView{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 60)];
    view.backgroundColor=UIColor.whiteColor;
    view.userInteractionEnabled = YES;
    [view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapLogout)]];
    UILabel * label = UILabel.new;
    label.text = @"退出登录";
    label.textColor = UIColorFromRGB(0xE93740);
    label.font = PingFangSC_Regular(18);
    label.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    
    return view;
}
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        NSArray * array = @[@[@"编辑资料",@"账号和隐私设置",@"黑名单"]
                            ,@[@"清除缓存",@"文章字体大小",@"消息通知",@"提示音开关"]
                            ,@[@"关于我们"]];
        _dataSource =[NSMutableArray array];
        [_dataSource addObjectsFromArray:array];
    }
    return _dataSource;
}
@end
